package com.ui.controls
{
	import com.ui.data.IconData;
	import com.ui.data.RadioButtonData;
	import com.ui.manager.UIManager;
	import com.utils.BDUtil;

	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;

	/**
	 * @version 20091215
	 * @author Cafe
	 */
	public class RadioButton extends ToggleBase
	{
		protected var _data:RadioButtonData;
		protected var _upSkin:DisplayObject;
		protected var _upIcon:BitmapData;
		protected var _overIcon:BitmapData;
		protected var _selected_upIcon:BitmapData;
		protected var _icon:Icon;
		protected var _label:Label;

		override protected function create():void
		{
			_upIcon = BDUtil.getBD( _data.upIcon );
			_overIcon = BDUtil.getBD( _data.overIcon );
			_selected_upIcon = BDUtil.getBD( _data.selectedUpIcon );
			var data:IconData = new IconData();
			data.x = _data.padding;
			_icon = new Icon( data );
			_selected = _data.selected;
			_icon.bitmapData = (_selected ? _selected_upIcon : _upIcon);
			_label = new Label( _data.labelData );
			if ( _data.upAsset)
			{
				_upSkin = UIManager.getUI( _data.upAsset );
				addChild( _upSkin );
			}
			addChild( _icon );
			addChild( _label );
		}

		override protected function layout():void
		{
			_label.x = _data.padding + _icon.width + _data.hGap;
			_width = _icon.width + _data.hGap + _label.width + _data.padding * 2;
			_height = Math.max( _icon.height, _label.height ) + _data.padding * 2;
			_icon.y = Math.floor( (_height - _icon.height) / 2 );
			_label.y = Math.floor( (_height - _label.height) / 2 );
			if (_upSkin)
			{
				_upSkin.width = _width;
				_upSkin.height = _height;
			}
		}

		protected function mouseDownHandler( event:MouseEvent ):void
		{
			if (!_enabled) return;
			if (_group)
			{
				if (!_selected) selected = true;
			}
			else
			{
				selected = !_selected;
			}
		}

		override protected function onSelect():void
		{
			_icon.bitmapData = (_selected ? _selected_upIcon : _upIcon);
		}

		override protected function onEnabled():void
		{
			_icon.gray = !_enabled;
			_label.enabled = _enabled;
		}

		protected override function onShow():void
		{
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			if (_data.overIcon)
			{
				addEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
				addEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
			}
		}

		protected function mouseOverHandler( event:MouseEvent ):void
		{
			if (!_selected)
				_icon.bitmapData = _overIcon;
		}

		protected function mouseOutHandler( event:MouseEvent ):void
		{
			_icon.bitmapData = (_selected ? _selected_upIcon : _upIcon);
		}

		protected override function onHide():void
		{
			removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			if (_data.overIcon)
			{
				removeEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
				removeEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
			}
		}

		public function RadioButton( data:RadioButtonData )
		{
			_data = data;
			super( data );
		}
	}
}
